package com.ff.javamonkey.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.util.Log;

public class JavaMonkeyPreferences {
	// Debugging
	private static final String TAG = "JavaMonkeyPreferences";
	
	private SharedPreferences prefs;
	
	// Visible Preference Keys
	public static final String PREFERENCE_IP_SETTING = "ip_setting";
	public static final String PREFERENCE_USER_SETTING = "user_setting";
	public static final String PREFERENCE_PASS_SETTING = "pass_setting";
	
	public static String CURR_PREF_IP_SETTING;
	public static String CURR_PREF_USER_SETTING;
	public static String CURR_PREF_PASS_SETTING;
	
	private static Resources mResources;
	
	public JavaMonkeyPreferences(Context context) {
		prefs = context.getSharedPreferences("FlightFocus", Context.MODE_WORLD_READABLE);
	}

	public static void setupDefaults(SharedPreferences preferences,
			Resources resources) {
		mResources = resources;
		Editor editor = preferences.edit();
		Log.i(TAG, "Default values: " + CURR_PREF_IP_SETTING + ", " + preferences.getString(PREFERENCE_IP_SETTING, ConnectionSettings.FDCU_PARENT_HOST));
		if (!preferences.contains(PREFERENCE_IP_SETTING)) {
			Log.i(TAG, "Entering if");
			editor.putString(PREFERENCE_IP_SETTING, ConnectionSettings.FDCU_PARENT_HOST);
			setIpSetting(ConnectionSettings.FDCU_PARENT_HOST);
		}
		else {
			setIpSetting(preferences.getString(CURR_PREF_IP_SETTING, ConnectionSettings.FDCU_PARENT_HOST));
		}
		if (!preferences.contains(PREFERENCE_USER_SETTING)) {
			editor.putString(PREFERENCE_USER_SETTING, ConnectionSettings.FDCU_PARENT_USER);
			setUserSetting(ConnectionSettings.FDCU_PARENT_USER);
		}
		else {
			setUserSetting(preferences.getString(CURR_PREF_USER_SETTING, ConnectionSettings.FDCU_PARENT_USER));
		}
		if (!preferences.contains(PREFERENCE_PASS_SETTING)) {
			editor.putString(PREFERENCE_PASS_SETTING, ConnectionSettings.FDCU_PARENT_PASS);
			setPassSetting(ConnectionSettings.FDCU_PARENT_PASS);
		}
		else {
			setPassSetting(preferences.getString(CURR_PREF_PASS_SETTING, ConnectionSettings.FDCU_PARENT_PASS));
		}
		editor.commit();
	}

	public static void setIpSetting(String host) {
		CURR_PREF_IP_SETTING = host;
		ConnectionSettings.FDCU_PARENT_HOST = CURR_PREF_IP_SETTING;
	}
	
	public static void setUserSetting(String user) {
		CURR_PREF_USER_SETTING = user;
		ConnectionSettings.FDCU_PARENT_USER = CURR_PREF_USER_SETTING;
	}
	
	public static void setPassSetting(String pass) {
		CURR_PREF_PASS_SETTING = pass;
		ConnectionSettings.FDCU_PARENT_PASS = CURR_PREF_PASS_SETTING;
	}
}
